package itsm.isperp.module.service.db;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.backup.DatabaseUtils;
import itsm.isperp.framework.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class DbDataManageService {

	@Autowired
	DbDataBackupLogService dbDataBackupLogService;

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	/**
	 * 清理系统数据
	 */
	public void clearBusinessData() {
		/**
		 * DELETE FROM `app_chat`; DELETE FROM `app_job_history`;
		 * 
		 * DELETE FROM `app_login_log`;
		 * 
		 * DELETE FROM `app_upload_file`;
		 * 
		 * 
		 * DELETE FROM `app_message`; DELETE FROM `app_message_failed`; DELETE
		 * FROM `app_message_receiver`; DELETE FROM `app_message_sender`; DELETE
		 * FROM `app_message_success`;
		 * 
		 * DELETE FROM `icd_incident`; DELETE FROM
		 * `icd_incident_handle_process`;
		 * 
		 * DELETE FROM `kng_knowledge_comment`; DELETE FROM
		 * `kng_knowledge_item`; DELETE FROM `kng_myfavorite`;
		 * 
		 * 
		 * DELETE FROM `slm_alarm_message_history`; DELETE FROM
		 * `slm_alarm_object`; DELETE FROM `slm_busobject_service_target`;
		 * DELETE FROM `slm_busobject_service_target_history`; DELETE FROM
		 * `slm_service_agreement`; DELETE FROM `slm_service_target`; DELETE
		 * FROM `slm_upgrade_setting`; DELETE FROM `app_user_custom_style`;
		 * DELETE FROM `app_user_webos_menu`;
		 * 
		 * 
		 * DELETE FROM `spt_sparepart`; DELETE FROM `spt_sparepart_back`; DELETE
		 * FROM `spt_sparepart_change_detail`; DELETE FROM
		 * `spt_sparepart_discard`; DELETE FROM `spt_sparepart_in`; DELETE FROM
		 * `spt_sparepart_in_detail`; DELETE FROM
		 * `spt_sparepart_min_stock_rule`; DELETE FROM `spt_sparepart_out`;
		 * DELETE FROM `spt_sparepart_in`;
		 * 
		 * 
		 * DELETE FROM `db_data_backup_log`;
		 * 
		 * 
		 * DELETE FROM `tsk_plan_task`; DELETE FROM `tsk_plantask_todo_mq`;
		 * DELETE FROM `tsk_task`
		 * 
		 **/

		String[] sql = { "DELETE FROM app_chat", "DELETE FROM app_job_history",

		"DELETE FROM app_login_log",

		"DELETE FROM app_upload_file",

		"DELETE FROM app_message", "DELETE FROM app_message_failed",
				"DELETE FROM app_message_receiver",
				"DELETE FROM app_message_sender",
				"DELETE FROM app_message_success",

				"DELETE FROM icd_incident",
				"DELETE FROM icd_incident_handle_process",

				"DELETE FROM kng_knowledge_comment",
				"DELETE FROM kng_knowledge_item", "DELETE FROM kng_myfavorite",

				"DELETE FROM slm_alarm_message_history",
				"DELETE FROM slm_alarm_object",
				"DELETE FROM slm_busobject_service_target",
				"DELETE FROM slm_busobject_service_target_history",
				"DELETE FROM slm_service_agreement",
				"DELETE FROM slm_service_target",
				"DELETE FROM slm_upgrade_setting",
				"DELETE FROM app_user_custom_style",
				"DELETE FROM app_user_webos_menu",

				"DELETE FROM spt_sparepart", "DELETE FROM spt_sparepart_back",
				"DELETE FROM spt_sparepart_change_detail",
				"DELETE FROM spt_sparepart_discard",
				"DELETE FROM spt_sparepart_in",
				"DELETE FROM spt_sparepart_in_detail",
				"DELETE FROM spt_sparepart_min_stock_rule",
				"DELETE FROM spt_sparepart_out",
				"DELETE FROM spt_sparepart_in",

				"DELETE FROM db_data_backup_log",

				"DELETE FROM tsk_plan_task",
				"DELETE FROM tsk_plantask_todo_mq", "DELETE FROM tsk_task",
				"DELETE FROM  ACT_RU_VARIABLE ",
				"DELETE FROM  ACT_RU_IDENTITYLINK ",
				"DELETE FROM  ACT_RU_TASK ", "DELETE FROM  ACT_RU_EXECUTION ",
				"DELETE FROM  ACT_ID_MEMBERSHIP ",
				"DELETE FROM  ACT_ID_GROUP ", "DELETE FROM  ACT_ID_USER ",
				"DELETE FROM  ACT_ID_INFO ", "DELETE FROM  ACT_RU_JOB ",
				"DELETE FROM  ACT_HI_PROCINST ",
				"DELETE FROM  ACT_HI_ACTINST ",
				"DELETE FROM  ACT_HI_TASKINST ", "DELETE FROM  ACT_HI_DETAIL ",
				"DELETE FROM  ACT_HI_COMMENT ",
				"DELETE FROM  ACT_HI_ATTACHMENT ",
				"DELETE FROM  ACT_RU_EVENT_SUBSCR " };

		jdbcTemplate.batchUpdate(sql);

	}

	/**
	 * 
	 * @throws SQLException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void backupData() throws SQLException, IOException,
			InterruptedException {

		String filePath = FileUtils.generateFilepath("backup", ".sql");
		String fileAbsolutePath = ContextHolder.getUploadFileStorePath()
				+ File.separator + filePath;
		File f = new File(fileAbsolutePath);
		DatabaseUtils.backup(jdbcTemplate, f);
		dbDataBackupLogService.save(f.getName(),
				ContextHolder.getUploadFileStore() + File.separator + filePath,
				(int) f.length());

	}
}
